package com.chase.champagne;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.WeekView;

public class MyWeekView extends WeekView {

    private int mRadius;

    public MyWeekView(Context context) {
        super(context);
//        //兼容硬件加速无效的代码
//        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
//        //4.0以上硬件加速会导致无效
//        mSelectedPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
//
//        setLayerType(View.LAYER_TYPE_SOFTWARE, mSchemePaint);
//        mSchemePaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
    }

    /**
     * 如果需要点击Scheme没有效果，则return true
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return false 则不绘制onDrawScheme，因为这里背景色是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        return true;
    }


    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {
        int cx = x + mItemWidth / 2;
        int cy = mItemHeight / 2;

        mSchemePaint.setStyle(Paint.Style.STROKE);//绘图为线条模式
        float strokeWidth = (float)(mRadius * 0.1);
        mSchemePaint.setStrokeWidth(strokeWidth);
        canvas.drawCircle(cx, cy, mRadius, mSchemePaint);
//        canvas.drawCircle(cx, cy, mRadius, mSchemePaint);
//
//        // 把中间部分擦除
//        mSchemePaint.setAlpha(0);
//        mSchemePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//        int mRingWidth = mRadius / 20;
//        canvas.drawCircle(cx, cy, mRadius-mRingWidth, mSchemePaint);
    }

    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int top = -mItemHeight / 8;
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() ? mCurDayTextPaint : mSelectTextPaint);
            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight / 10,
                    calendar.isCurrentDay() ? mCurDayLunarTextPaint : mSelectedLunarTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mSchemeTextPaint);

            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight / 10, mSchemeLunarTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mCurMonthTextPaint);
            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight / 10,
                    calendar.isCurrentDay() ? mCurDayLunarTextPaint : mCurMonthLunarTextPaint);
        }
    }
}


//public class MyWeekView extends WeekView {
//    private Paint mTextPaint = new Paint();
//    private Paint mSchemeBasicPaint = new Paint();
//    private float mRadio;
//    private int mPadding;
//    private float mSchemeBaseLine;
//
//    public MyWeekView(Context context) {
//        super(context);
//
//        mTextPaint.setTextSize(dipToPx(context, 8));
//        mTextPaint.setColor(0xffffffff);
//        mTextPaint.setAntiAlias(true);
//        mTextPaint.setFakeBoldText(true);
//
//        mSchemeBasicPaint.setAntiAlias(true);
//        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
//        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
//        mSchemeBasicPaint.setColor(0xffed5353);
//        mSchemeBasicPaint.setFakeBoldText(true);
//        mRadio = dipToPx(getContext(), 7);
//        mPadding = dipToPx(getContext(), 4);
//        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
//        mSchemeBaseLine = mRadio - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);
//
//    }
//
//    /**
//     *
//     * @param canvas    canvas
//     * @param calendar  日历日历calendar
//     * @param x         日历Card x起点坐标
//     * @param hasScheme hasScheme 非标记的日期
//     * @return true 则绘制onDrawScheme，因为这里背景色不是是互斥的
//     */
//    @Override
//    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
//        mSelectedPaint.setStyle(Paint.Style.FILL);
//        mSelectedPaint.setColor(0x80cfcfcf);
//        canvas.drawRect(x + mPadding, mPadding, x + mItemWidth - mPadding, mItemHeight - mPadding, mSelectedPaint);
//        return true;
//    }
//
//    @Override
//    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {
//        mSchemeBasicPaint.setColor(calendar.getSchemeColor());
//
//        canvas.drawCircle(x + mItemWidth - mPadding - mRadio / 2, mPadding + mRadio, mRadio, mSchemeBasicPaint);
//
//        canvas.drawText(calendar.getScheme(), x + mItemWidth - mPadding - mRadio, mPadding + mSchemeBaseLine, mTextPaint);
//    }
//
//    @Override
//    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
//        int cx = x + mItemWidth / 2;
//        int top = -mItemHeight / 6;
//
//        if (isSelected) {
//            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
//                    mSelectTextPaint);
//            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight / 10, mSelectedLunarTextPaint);
//        } else if (hasScheme) {
//            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
//                    calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
//
//            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight / 10, mCurMonthLunarTextPaint);
//        } else {
//            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
//                    calendar.isCurrentDay() ? mCurDayTextPaint :
//                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
//            canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + mItemHeight / 10,
//                    calendar.isCurrentDay() ? mCurDayLunarTextPaint :
//                            calendar.isCurrentMonth() ? mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);
//        }
//    }
//
//    /**
//     * dp转px
//     *
//     * @param context context
//     * @param dpValue dp
//     * @return px
//     */
//    private static int dipToPx(Context context, float dpValue) {
//        final float scale = context.getResources().getDisplayMetrics().density;
//        return (int) (dpValue * scale + 0.5f);
//    }
//}